//
//  collectViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/30.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "collectViewController.h"
#import "CollectionWebViewModel.h"
#import "singleNextPageViewController.h"

@interface collectViewController ()
@property (nonatomic, retain) NSMutableArray *arrOfData;


@end

@implementation collectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"收藏";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    [self dataHandel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrOfData.count;
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    
    cell.textLabel.text = [_arrOfData[indexPath.row]title];
    
    
    
    return cell;
    
}

- (void)viewDidAppear:(BOOL)animated{

    if (self.arrOfData.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"没有收藏" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        [self performSelector:@selector(dismiss) withObject:alert afterDelay:1];
        
        
    }
}
- (void)dataHandel{
    // 解档
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"collectionWebView.txt"];
    self.arrOfData = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    singleNextPageViewController *single = [[singleNextPageViewController alloc]init];

    single.url2 = [_arrOfData[indexPath.row]urlStr];
    
    
    [self.navigationController pushViewController:single animated:YES];
    
    
    

}
- (void)dismiss{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_arrOfData removeObjectAtIndex:indexPath.row];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"collectionWebView.txt"];
        // 归档
        [NSKeyedArchiver archiveRootObject:_arrOfData toFile:path];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}



@end
